package cn.tedu._06vehicle.base.exception;

import cn.tedu._06vehicle.base.response.JsonResult;
import cn.tedu._06vehicle.base.response.StatusCode;
import jakarta.validation.ConstraintViolationException;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

/**
 * ControllerAdvice注解:
 *   1.标识该类为全局异常处理器的类;
 *   2.当控制器抛出异常时,框架会优先检查是否定义了全局异常处理器;
 * RestControllerAdvice注解:
 *   1.组合注解,等价于@ControllerAdvice + @ResponseBody;
 *   2.添加该注解后,会为每一个异常处理方法上添加 @ResponseBody 注解;
 */


@Slf4j
//@ControllerAdvice
@RestControllerAdvice
//@RestControllerAdvice组合注解，等价于@ControllerAdvice和@ResponseBody，添加该注解后，控制器方法返回的数据会自动封装成JSON数据
public class GlobalExceptionHandler {

    /**
     * @ExceptionHandler注解 标识该方法是异常的处理方法；
     */

    /**第一个异常处理方法：演示*/
    @ExceptionHandler(IllegalArgumentException.class)
    public JsonResult doHandlerIllegalArgumentException(IllegalArgumentException exception){
        //ex.getMessage():获取异常提示消息
        String data=exception.getMessage();
        log.error("IllegalArgumentException:"+data);
        //{"state":3000, "message":"操作失败", "data": "车辆id不能小于0"}
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }
    @ExceptionHandler(RuntimeException.class)
    public JsonResult doHandlerRuntimeException(RuntimeException exception){
        String data=exception.getMessage();
        log.error("RuntimeException:"+data);
        return new JsonResult(StatusCode.OPERATION_FAILED,data);
    }

    /**
     * @Validation:封装参数验证失败的异常处理方法
     * 处理参数验证失败的异常
     *
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public JsonResult daHandlerMethodArgumentNotValidException
            (MethodArgumentNotValidException exception ){
        String data=exception.getFieldError().getDefaultMessage();
        log.error("MethodArgumentNotValidException:"+data);
        return new JsonResult(StatusCode.VALIDATED_ERROR, data);
    }

    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult doHandlerConstraintViolationException(ConstraintViolationException ex){
        String date=ex.getConstraintViolations().iterator().next().getMessage();
        log.error("ConstraintViolationException:"+date);
        return new JsonResult(StatusCode.VALIDATED_ERROR,date);
    }

    /**
     * 异常处理方法
     * 文件上传超出大小限制的异常
     */
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public JsonResult doHandleMaxUploadSizeExceededException(MaxUploadSizeExceededException ex){
        String date=ex.getMessage();
        log.error("MaxUploadSizeExceededException:"+date);
        return new JsonResult(StatusCode.OPERATION_FAILED,date);
    }




    /**
     * 能够处理所有异常的异常处理方法
     * 一般最后添加
     */

//    @ExceptionHandler(Throwable.class)
//    public JsonResult doHandlerException(Throwable throwable){
//        String data=throwable.getMessage();
//        log.error("未知异常:"+data);
//        return new JsonResult(StatusCode.THROWABLE_ERROR,data);
//    }
}
